package org.FaPe3D.core.viewports
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import flash.events.*;
	import flash.utils.Dictionary;
	
	import org.FaPe3D.*;
	import org.FaPe3D.core.events.EventManager;
	import org.FaPe3D.core.parsers.ViewPort;
	import org.FaPe3D.core.settings.*;
	
	public class ViewPorts extends EventManager
	{
		private var _id:String;
		private var _viewportPath:String;
		private var _bulkLoader:BulkLoader;
		private var _LOADER_KEY = 'viewport';
		private var _viewport:Dictionary = new Dictionary(true);
		private var _viewportParser:ViewPort;
		private var _defaultViewportKey:String = '';
		private var _xmlBase:XML;
		
		public function ViewPorts(id:String, viewportPath:String)
		{
			_id = id;
			_viewportPath = viewportPath;
			_LOADER_KEY = new Dictionary(true);
			_LOADER_KEY = _LOADER_KEY + _id;
			_viewportParser = new ViewPort(_id);
			_defaultViewportKey = Adapter.getAdapter(_id).settings.setting['property.initialviewport'];
			
			// cretae bulkLoader instance
			_bulkLoader = new BulkLoader('FAPE3D_' + 'viewpors_' + _id);
			
			// add file to bulk loader
			_bulkLoader.add( viewportPath, {type:"xml", id: _LOADER_KEY});
			
			// start loading
			_bulkLoader.start();
			// setup events
			_bulkLoader.addEventListener(BulkProgressEvent.COMPLETE, onCompelete);
			_bulkLoader.addEventListener(BulkLoader.PROGRESS, loadingProgress);
			_bulkLoader.addEventListener(BulkLoader.ERROR, loadingError)
		}
		
		private function onCompelete(e:*):void
		{
			// get content and clear memory
			_xmlBase = _bulkLoader.getXML( _LOADER_KEY, true );
			//
			var externalElements:ExternalElements = new ExternalElements(_id, _xmlBase);
			externalElements.addEventListener( Event.COMPLETE, function()
			{
				_xmlBase = externalElements.xml;
				getExternalAppendObjects();
			});
		}
		
		private function getExternalAppendObjects():void
		{
			var externalAppendObjects:ExternalAppendObjects = new ExternalAppendObjects(_id, _xmlBase);
			externalAppendObjects.addEventListener(Event.COMPLETE, function()
			{
				_xmlBase = externalAppendObjects.xml;
				startParsing();
			});	
		}
		
		private function startParsing()
		{
			_viewportParser.parse( _xmlBase , null);
			
			// view port parser listener
			_viewportParser.addEventListener(Event.COMPLETE, onViewPortParsingCompelete);
		}
		
		
		private function onViewPortParsingCompelete(e:Event):void
		{
			dispatchEvent(new Event(Event.COMPLETE));
			
			// remove bulkloader instance
			_bulkLoader.removeAll();
		}
		
		public function get viewport():Dictionary
		{
			return _viewport;
		}
		
		public function set viewport(dic:Dictionary):void
		{
			_viewport = dic;
		}
		
		public function setViewports(key:String, value:*):void
		{
			_viewport[key] = value;
		}
		
		public function getViewports(key:String):*
		{	
			return _viewport[key];
		}
		
		public function getDefaultViewport():*
		{
			return getViewports(_defaultViewportKey);
		}
		
		
		private function loadingProgress(e:*):void
		{
//			trace('loading progress', e.bytesLoaded , '/', e.bytesTotal);
		}
		
		private function loadingError(e:*):void
		{
			trace('error in loading xml file. (viewport)' , e);
		}

	}
}